from io import BytesIO
from selenium import webdriver
from PIL import Image
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from HomeWorkday6.compare_helper import get_compare


# 全局小图尺寸


size = {'height': 76, 'width': 76}

browser = webdriver.Chrome()
# 设置浏览器窗口大小
browser.set_window_size(1400, 1400)
# 设置隐式等待对象
wait = WebDriverWait(browser, 5)

def get_page():
    url = 'http://www.1kkk.com/'
    browser.get(url)
    time.sleep(2)


#  点击头像登录
    browser.find_element_by_class_name('header-avatar').click()
    time.sleep(1)

    # 点击同意按钮
    agree_button = browser.find_elements_by_xpath('//div[@id="sy-win"]/div/div/a[2]')[0]
    agree_button.click()
    time.sleep(1)

#     输入手机和密码
    tel_input = browser.find_elements_by_xpath('//div[@class="form-wrap"]/p[2]/input')[0]
    tel_input.clear()
    tel_input.send_keys('15029281055')

    password_input = browser.find_elements_by_xpath('//div[@class="form-wrap"]/p[3]/input')[0]
    password_input.clear()
    password_input.send_keys('1kkkcode')

    time.sleep(1)

    # 截取大图,并保存。
    screenshot = browser.get_screenshot_as_png()
    full_image = Image.open(BytesIO(screenshot))
    full_image.save('./full_image.png')
   # 获取验证码位置保存4张验证码图片
    for x in range(1, 5):
       # img = wait.until(EC.presence_of_element_located((By.XPATH, './/div[@class="rotate-background"][%s]' % x)))
       # print(img.location)
       # left_x, left_y = img.location['x'], img.location['y']
       # left_x *= 1.24691358
       # left_y *= 1.24574209
       # print(left_x,left_y)
       if x == 1:
           left_x = 607
           left_y = 511
           right_x, right_y = left_x + size['width'], left_y + size['height']
       else:
            left_x = 607  + 77 * (x-1) + x
            left_y = 511
            right_x, right_y = left_x + size['width'], left_y + size['height']
       captcha_image = full_image.crop((left_x, left_y, right_x, right_y))
       captcha_image.save('./captcha%s.png' % x)

    return browser


def click_captcha(browser, captcha_num: int):
    '''获取节点并点击'''

    browser.find_element_by_xpath('.//div[@class="rotate-background"][%s]' % captcha_num).click()

def check_result(browser):
    '''验证码图片和图库进行对比'''
    # 图库地址
    file_name = './cut_images/%s.png'
    # 验证图片地址
    captcha_name = './captcha%s.png'
    for i in range(1, 5):
        file_name1 = captcha_name % i

        flag = True
        while flag:
            # 小于85执行点击函数
            click_captcha(browser, i)
            # 全屏截图，生成一个图片流
            screenshot = browser.get_screenshot_as_png()
            # 生成image对象，便于后面裁剪
            full_image = Image.open(BytesIO(screenshot))
            full_image.save('./full_image.png')

            # img = wait.until(
            #             #     EC.presence_of_element_located((By.XPATH, './/div[@class="rotate-background"][%s]' % i)))
            #             # # print(img.location)
            #             # left_x, left_y = img.location['x'], img.location['y']

            # 显示端因分辨率不同
            if i == 1:
                left_x = 607
                left_y = 514
                right_x, right_y = left_x + size['width'], left_y + size['height']
            else:
                left_x = 607 + 77 * (i-1) + i
                left_y = 514
                right_x, right_y = left_x + size['width'], left_y + size['height']

            captcha_image = full_image.crop((left_x, left_y, right_x, right_y))
            captcha_image.save('./captcha%s.png' % i)

            for j in range(1, 152):
                file_name2 = file_name % j
                differences = get_compare(file_name1, file_name2)
                if differences > 85:
                    flag = False


def main():
    browser = get_page()
    check_result(browser)



if __name__ == '__main__':
    main()

